package com.zhoug.zmui.round;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.alpha.ZMUIAlphaImageView;
import com.zhoug.zmui.utils.ZMUIViewHelper;

/**
 * 圆形图片 :遮罩实现,
 * 优点:无毛边
 * 缺点:可能会出现黑色背景的bug
 * bug:在RelativeLayout中是尺寸计算会出问题,解决版本在ZMUIRoundImageView外面包一层FrameLayout
 * @Author 35574
 * @Date 2020/7/24
 * @Description
 */
public class ZMUIRoundImageView extends ZMUIAlphaImageView {
    private static final String TAG = ">>>ZMUIRoundImageView";
    private static  boolean debug = false;

    private ZMUIViewHelper _mRoundViewHelper;

    public static void setDebug(boolean debug) {
        ZMUIRoundImageView.debug = debug;
    }

    public ZMUIRoundImageView(Context context) {
        this(context, null);
    }

    public ZMUIRoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIRoundImageView);
    }

    public ZMUIRoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.zmui_style_ZMUIRoundImageView);
    }

    public ZMUIRoundImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        _getZMUIViewHelper().parseAttribute(context, attrs, defStyleAttr, defStyleRes);
       if(isRound()){
           setScaleType(ScaleType.CENTER_CROP);
       }
    }

    private ZMUIViewHelper _getZMUIViewHelper(){
        if(_mRoundViewHelper==null){
            _mRoundViewHelper = new ZMUIViewHelper(this);
        }
        return _mRoundViewHelper;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (debug) {
            LogUtils.d(TAG, "onSizeChanged:w="+w+",h="+h);
        }
        _getZMUIViewHelper().onSizeChanged(w, h);
    }


    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        _getZMUIViewHelper().onPressedChanged(this,pressed);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        _getZMUIViewHelper().onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (debug) {
            LogUtils.d(TAG, "onMeasure:width="+width+",height="+height);
            LogUtils.d(TAG, "onMeasure:MeasuredWidth="+getMeasuredWidth()+",MeasuredHeight="+getMeasuredHeight());
        }
        int size = _getZMUIViewHelper().measureSize(getMeasuredWidth(), getMeasuredHeight());
        if (size > 0) {
            setMeasuredDimension(size, size);
        }
    }

    public boolean isRound() {
        return   _getZMUIViewHelper().isRound();
    }

    public void setRound(boolean round) {
        _getZMUIViewHelper().setRound(round);
    }

    public void setCorners(int corners) {
        _getZMUIViewHelper().setCorners(corners);
    }

    public int getCorners() {
        return   _getZMUIViewHelper().getCorners();
    }

    public int getBorder() {
        return   _getZMUIViewHelper().getBorder();
    }

    public void setBorder(int border) {
        _getZMUIViewHelper().setCorners(border);
    }

    public int getBorderColor() {
        return   _getZMUIViewHelper().getBorderColor();
    }

    public void setBorderColor(int borderColor) {
        _getZMUIViewHelper().setBorderColor(borderColor);
    }

}
